<?php


class Crawl_tencent_keyword_from_client_library  extends Crawl_keyword_library{

    private $_url;
    private $_cookie;
    private $_parameters = '';
    public  $_htmlsearch_config;
    public  $_null_tab;

    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['tencent_client'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];
        $this->_no_login    = $this->_htmlsearch_config['no_login'];
        $this->_restricted_tag = $this->_htmlsearch_config['restricted_tag'];
        $this->_illegal_tag  = $this->_htmlsearch_config['illegal_tag'];
        $this->_result_error = $this->_htmlsearch_config['result_error'];
        $this->_result_success = $this->_htmlsearch_config['result_success'];
        $this->_url         = $this->_htmlsearch_config['url'];
        $this->_pos         = $this->_htmlsearch_config['pos'];
        $this->_s_dup       = $this->_htmlsearch_config['s_dup'];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {
        $crawl_result = $this->try_crawl($retry);


        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } elseif(is_null($crawl_result)) {
            return array('data' => NULL);
        }

        return $this->make_data($crawl_result);
    }


	/**
	* xxxxxxx
	*
	* @access public
	* @param string $result 腾讯微博给出的response..此处为json串 [Must]
	* @return array $mixed
	*/
    private function make_data($result = NULL) {
        $this->_ci->load->model('split/htmlsearchparser_model');
        $split_data = $this->_ci->htmlsearchparser_model->parser_tencent_client($this->_htmlsearch_config['source_myid'], $result);
        if($split_data == FALSE) {
            $error = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data',$result);
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }

        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */
    private function try_crawl($retry = FALSE) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters();

        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Tencent Client Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }

        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Tencent Client Un Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            return self::get_content($parameters);
        } else {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['tencent_client'], TRUE);
			if(is_array($crawl_use_proxy_ip_result) && array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['tencent_client'], FALSE);
            } else {
                return $crawl_use_proxy_ip_result;
            }
        }
    }


    /**
    * 处理抓取后的结果
    *
    * 检查判断是否返回是否有错误
    * 检查判断是否为空
    *
    * @access public
    * @param array $crawl_result xxxxx [Must]
    * @param string $use_proxy xxxxxx [Optional]
    * @return string $mixed 腾讯微博给出的response..此处为json串
    */
    public function process_crawl_result($crawl_result, $use_proxy = self::PRROXY_IP) {
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        }

        $response_array = json_decode($crawl_result['result'],true);

        //腾讯错误 返回  {"result":"-1","msg":"操作异常"}
        if($response_array['result'] == -1){
            $error = $this->_ci->error_code_library->getError('system', 'data', 'return_error', $crawl_result['result']);
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }

        //返回 noResultData 标识没有数据
        $no_data = $response_array['noResultData'];

        //返回记录总数
        $totalCount = $response_array['info']['totalCount'];

        //返回本页记录总数
		$response_data = $response_array['jsonDump']['msgs'];
		$response_data_count = count($response_data);

        if($no_data || ($totalCount < 1) || ($response_data_count < 1)) {
            return NULL;
        }

        return $crawl_result['result'];
    }


    private function make_parameters() {

//http://ti.3g.qq.com/touch/s?sid=AUDaW8WDwRz6qV5bKkPgSwkn&aid=vaction&mst=33&ac=60&shinfo=txt.wap_paged.l&shinforen=txt.wap_paged.k&keyword=%E5%9B%9E%E9%BE%99%E8%A7%82&&&dl2=1&dumpJSON=1&pageid=search&g_f=18106&params=keyword%2C%E5%9B%9E%E9%BE%99%E8%A7%82%7Ctype%2Cmsg&pid=1&psize=30

  		$keyword = trim($this->_ci->input->post('word'));
        $parameters['url'] = $this->_url;
        $parameters['method'] = self::METHOD_GET;
        $parameters['postData']['pid'] = $this->get_page();
        $parameters['postData']['psize'] = 30;

        $this->_ci->load->helper('string');
        $sid = random_string('alnum', 24);
        $qq = random_string('numeric', 9);
        $pgv_pvid = random_string('numeric', 10);

        $pt_sccv = time().'3421';

        $ptcz = random_string('alnum', 64);
        $ptcz = strtolower($ptcz);

        $lskey = random_string('alnum', 88);
        $lskey = strtolower($lskey);

  		//var_dump($sid);

        //$parameters['postData']['sid'] = 'AUDaW8WDwRz6qV5bKkPgSwkn';
        $parameters['postData']['sid'] = $sid;
        $parameters['postData']['aid'] = 'vaction';
        $parameters['postData']['mst'] = 33;
        $parameters['postData']['ac'] = 60;
        $parameters['postData']['shinfo'] = 'txt.wap_paged.l';
        $parameters['postData']['shinforen'] = 'txt.wap_paged.k';
        $parameters['postData']['keyword'] = $keyword;
        $parameters['postData']['dl2'] = 1;
        $parameters['postData']['dumpJSON'] = 1;
        $parameters['postData']['pageid'] = 'search';
        $parameters['postData']['g_f'] = 18106;
        $parameters['postData']['params'] = "keyword,{$keyword}|type,msg";

        $parameters['cookie_str'] = true;
        $parameters['cookie'] = "cookie: pgv_pvid={$pgv_pvid}; pt2gguin=o{$qq}; RK=Z7Pq8LM139; ptcz={$ptcz}; luin=o{$qq}; lskey={$lskey}; o_cookie={$qq}; pgv_pvi={$pgv_pvid}; g_ut=3; pt_ccv=61CF8D0C2C02D0DB37A7D0CB722BA538!7KY7IgM2f9!{$pt_sccv}; pt_sccv={$pt_sccv}!e408af16-2df7-4685-8103-4284a17d1816!1929247271; 3g_lastLoginQq={$qq}; ng_st_sid_v2=Xlwf85DPpx4.; ng_pt_his={$qq}|http%3A%2F%2Fq4.qlogo.cn%2Fg%3Fb%3Dqq%26k%3DYaFfRfc4rW1Lwe98RWv1Gw%26s%3D40%26t%3D534!; mb_imgshow=show; mblog_listimg=true; nojump=false; JSESSIONID={$sid}";

        return $this->_parameters = $parameters;
    }

}